//
//  ProvincesPickView.m
//  省市区封装
//
//  Created by 徐宏飞 on 16/5/3.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "ProvincesPickView.h"
#import "XHF_areasPick.h"

#define WID [[UIScreen mainScreen] bounds].size.width / 375

@interface ProvincesPickView ()<XHF_areasPickDelegate>

@property (nonatomic, strong) XHF_areasPick *areasPick;
@property (nonatomic, strong) UIButton *tapCloseButton;
@property (nonatomic, assign) BOOL openState;

@end


@implementation ProvincesPickView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    
    [self prepareingLeftView];
    
    [self openingLeftController];
}

#pragma mark -- 准备菜单视图
- (void)prepareingLeftView{
    
    if (!_tapCloseButton) {
        self.tapCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.tapCloseButton.frame =[UIScreen mainScreen].bounds;
        self.tapCloseButton.backgroundColor = [UIColor lightGrayColor];
        self.tapCloseButton.alpha = 0;
        [self.tapCloseButton addTarget:self action:@selector(closeingLeftControllerWithMsg:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.tapCloseButton];
    }
    
    if (!_areasPick) {
        
        _areasPick = [[[NSBundle mainBundle] loadNibNamed:@"XHF_areasPick" owner:self options:nil]firstObject];
        _areasPick.frame = CGRectMake( 0, 667*WID, 375*WID, 367*WID);
        _areasPick.delegete = self;
        [self showShadow:YES];  //阴影效果
        [self addSubview:self.areasPick];
        self.openState = NO;
    }
}

#pragma mark -- 打开菜单视图
- (void)openingLeftController {
    
    
    if (self.openState == YES) {
        return;
    }
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.areasPick.frame = CGRectMake(0, 300*WID, 375*WID, 367*WID);
        self.tapCloseButton.alpha = 0.3;
        
    } completion:^(BOOL finished) {

        self.openState = YES;
        
    }];
    
}

#pragma mark -- 关闭菜单视图
- (void)closeingLeftControllerWithMsg:(NSString *)msg {
    
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.areasPick.frame = CGRectMake(0, 667*WID, 414*WID, 367*WID);
        self.tapCloseButton.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [self.areasPick removeFromSuperview];
        self.areasPick = nil;
        [self.tapCloseButton removeFromSuperview];
        self.tapCloseButton = nil;
        self.openState = NO;
        
        if ([msg isKindOfClass:[UIButton class]]) {
            [self.delegete disMissWithMessage:@"请选择市区"];
        } else {
            [self.delegete disMissWithMessage:msg];
        }
        
        
    }];
    
    [self showShadow:NO];
}

#pragma mark -- 阴影效果
- (void)showShadow:(BOOL)val {
    if (!self.areasPick) return;
    
    self.areasPick.layer.shadowOpacity = val ? 0.8f : 0.0f;
    if (val) {
        self.areasPick.layer.cornerRadius = 4.0f;
        self.areasPick.layer.shadowOffset = CGSizeZero;
        self.areasPick.layer.shadowRadius = 4.0f;
        self.areasPick.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    }
    
}

- (void)disMissCurrentPageWithMessage:(NSString *)msg{
    [self closeingLeftControllerWithMsg:msg];

}

@end








